package org.triumphxx.springboot.eventlistener.controller;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.triumphxx.springboot.eventlistener.event.LoginEvent;
import org.triumphxx.springboot.eventlistener.event.RegisterEvent;
import org.triumphxx.springboot.eventlistener.service.LoginService;

import javax.annotation.Resource;

/**
 * @author: wangyp
 * @Date: 2023/7/14
 * @Time: 10:09
 * @Desc: 测试事件接口
 **/
@RestController
public class EventController {

    @Resource
    LoginService loginService;
    @Resource
    private ApplicationEventPublisher applicationContext;

    @GetMapping("/login")
    private String testLoginEvent(){
        //登陆的逻辑
        loginService.login("张三");
        //登陆完成后发布登陆后的事件
        applicationContext.publishEvent(new LoginEvent("张三"));
        return "success";
    }

    @GetMapping("/register")
    private String testRegisterEvent(){
        applicationContext.publishEvent(new RegisterEvent("张三"));
        return "success";
    }
}

